// pages/list/index.js
var my = require("../../../utils/MyUtils")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag: '',
    listMain: [],
    listTitles: [],
    show: true
  },
  /**
   * 搜索
   * @param {*} e 
   */
  seach(e) {
    var t = this
    var _TOKEN = wx.getStorageSync('_TOKEN')
    if (e.detail.value != '') {
      my.request("/oa-applet/contacts-search", "get", {
        user_name: e.detail.value
      }, {
        token: _TOKEN
      }, function (res) {
        wx.hideLoading()
        if (res.data.code == 0) {
          if (res.data.data.length > 0) {
            t.setData({
              listMain: res.data.data
            })
          }
        }
      })
    } else {
      my.request("/oa-applet/contacts-list", "get", {}, {
        token: _TOKEN
      }, function (res) {
        wx.hideLoading()
        if (res.data.code == 0) {
          t.setData({
            listMain: res.data.data
          })
        }
      })
    }
  },

  ck(e) {
    var _TOKEN = wx.getStorageSync('_TOKEN')
    var t = this
    var list = this.data.listMain
    for (var i = 0; i < list.length; i++) {
      if (list[i].uid == e.currentTarget.dataset.id) {
        if (this.data.flag == "cs") {
          app.globalData.csr = list[i]
        } else {
          app.globalData.spr = list[i]
        }
      }
    }
    wx.navigateBack({
      delta: 0,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var t = this
    var url = ''
    var show = false
    var _TOKEN = wx.getStorageSync('_TOKEN')
    if (options.flag == "cs") {
      url = "/oa-applet/get-copyer"
    } else {
      url = "/oa-applet/get-approver"
      show = true
    }
    my.request(url, "get", {}, {
      token: _TOKEN
    }, function (res) {
      wx.hideLoading()
      t.setData({
        listMain: res.data.data,
        flag: options.flag,
        show
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  onHide: function () {

  },
})